package com.seal.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.seal.comm.utils.Page;
import com.seal.dao.BaseDaoImpl;
import com.seal.dao.IVerifybookDao;
import com.seal.dto.VerifybookDto;
import com.seal.model.Verifybook;

@Repository("verifybookDaoImpl")
@SuppressWarnings("unchecked")
public class VerifybookDaoImpl extends BaseDaoImpl implements IVerifybookDao {
	
	public void addVerifybook(Verifybook ca) throws Exception {
		this.save(ca);
	}

	public List<Verifybook> getVerifybookList(VerifybookDto dto, Page page){
		StringBuffer hql = new StringBuffer("from Verifybook t where 1=1");
		if (null != dto.getFromDate()) {
			hql.append(" and t.verifyDate >= :fromDate");
		}

		if (null != dto.getToDate()) {
			hql.append(" and t.verifyDate <= :toDate");
		}
		
		if (null != dto.getVflag()) {
			hql.append(" and t.vflag = :vflag");
		}
		
		if (null != dto.getCompanyId()) {
			hql.append(" and t.companyId = :companyId");
		}
		
		if (null != dto.getVerifyUserid()) {
			hql.append(" and t.verifyUserid = :verifyUserid");
		}

		Query query = this.getSession().createQuery(hql.toString());
		query.setProperties(dto);
		if (null != page) {
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getPageSize());
			String countSql = "select count(t) " + hql.toString();

			Query queryCount = this.getSession().createQuery(countSql);
			queryCount.setProperties(dto);
			page.setTotalNumber(Integer.parseInt(queryCount.uniqueResult().toString()));
		}

		return query.list();
	}

	
}
